package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.BlogMapper;
import com.ruoyi.system.domain.Blog;
import com.ruoyi.system.service.IBlogService;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * blogService业务层处理
 * 
 * @author mz
 * @date 2024-07-23
 */
@Service
public class BlogServiceImpl implements IBlogService 
{
    @Autowired
    private BlogMapper blogMapper;

    /**
     * 查询blog
     * 
     * @param blogId blog主键
     * @return blog
     */
    @Override
    public Blog selectBlogByBlogId(Long blogId)
    {
        return blogMapper.selectBlogByBlogId(blogId);
    }

    /**
     * 查询blog列表
     * 
     * @param blog blog
     * @return blog
     */
    @Override
    public List<Blog> selectBlogList(Blog blog)
    {
        return blogMapper.selectBlogList(blog);
    }

    /**
     * 新增blog
     * 
     * @param blog blog
     * @return 结果
     */
    @Override
    public int insertBlog(Blog blog)
    {
        blog.setCreateTime(DateUtils.getNowDate());
        return blogMapper.insertBlog(blog);
    }

    /**
     * 修改blog
     * 
     * @param blog blog
     * @return 结果
     */
    @Override
    public int updateBlog(Blog blog)
    {
        blog.setUpdateTime(DateUtils.getNowDate());
        return blogMapper.updateBlog(blog);
    }

    /**
     * 批量删除blog
     * 
     * @param blogIds 需要删除的blog主键
     * @return 结果
     */
    @Override
    public int deleteBlogByBlogIds(Long[] blogIds)
    {
        return blogMapper.deleteBlogByBlogIds(blogIds);
    }

    /**
     * 删除blog信息
     * 
     * @param blogId blog主键
     * @return 结果
     */
    @Override
    public int deleteBlogByBlogId(Long blogId)
    {
        return blogMapper.deleteBlogByBlogId(blogId);
    }


    /**
     * 根据状态获取数据
     * @param blogStatus 博文状态
     * @return 博文集合
     */
    @Override
//    @Transactional(rollbackFor = Exception.class)
    public List<Blog> selectBlogByStatus(String blogStatus) {

        LoginUser sysUser = SecurityUtils.getLoginUser();
        Blog aaa = new Blog();
        aaa.setUpdateBy(sysUser.getUsername());
        aaa.setBlogStatus(blogStatus);

        List<Blog> blist = blogMapper.selectBlogByStatus(aaa);
        return blist;
    }

    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    @Override
    public void xxxx(Blog param0) {

        int r = blogMapper.yyyyy(param0);

        if (r != 1) {
            throw new ServiceException("更新条数不正确，请联系管理员");
        }
    }

    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
    @Override
    public void modifyAllIds(List<Blog> param0) {
        for (Blog item: param0) {
            xxxx(item);
        }
    }
}
